<!--组件-AI启动输入框-->
<template>
  <div>
    <VInput class="input" v-model="investCoinnNum" :isEdit="isEdit" :title="title" :explain="explain" :placeholder="T('总投入资金')" :optional="true" :min="minInvest" :max="maxInvest" 
    :unit="investCoin" @input="onInput"></VInput>

    <WeexSlide class="slide" v-if="slideParams && isEdit && isSlide" :params="slideParams"  @slideChange="slideChange"></WeexSlide>
    
    <div class="row" v-if="isEdit">
      <text class="font24 text3" v-if="propose.length">{{T('建议投入')+' '+propose+' '+investCoin}}</text>
      <div class="all"></div>
      <text class="font24 text3">{{T('可用')+' '+moneyNum+' '+investCoin}}</text>
    </div>
  </div>
</template>

<script>

//组件
import VInput from "@/public/view/VInput.vue";

//功能
import math from "@/_js/math";
import modal from "@/_js/modal";
import API from "@/_js/api";
import mLimit from "@/up/amLimit";

const weexFunction = weex.requireModule('WeexFunction');

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {

  components: {
    VInput,
  },

  model:{
    prop:"investCoinnNum",
    event:"change",
  },
  
  props:{
    investCoinnNum:String, //投资币数量
    
    title: { //标题
      type:String,
      default:T('总投入资金'),
    },

    explain: { //说明
      type:String,
      default:undefined,
    },

    isEdit:{ //true支持编辑
      type:Boolean,
      default:true,
    }, 

    isSlide:{ //true支持滑条
      type:Boolean,
      default:true,
    },

    isMax:{ //true最大值
      type:Boolean,
      default:false,
    },

    moneyType:{ //资金类型 1普通 2总数
      type:Number,
      default:1,
    },
    api:Object, //选中API
    investCoin:String,//投资币
    pairMap:Object, //选中的交易对
    params:Object, //参数数组（投入限制联动使用）
    pairNum:Number, //币对数量
    limitMap:Object, //投入限制

    type:String, //ai(ai启动) define(自定义启动) many(多币对启动)
    name:String, //网格、马丁、海浪、衡量定投、其他
  },

  watch: {
    
    api: { //APi改变，更新可用余额
      deep: true,
      handler (val, oldVal) {
        this.upApiMoney();
        this.upSlideBar();
      }
    },

    params:{ //参数改变，更新投入限制
      deep: false,
      handler (val, oldVal) {
        this.upInvestLimit();
      }
    },
    
    pairMap: { //交易对改变，更新投资限制、可用余额
      deep: false,
      handler (val, oldVal) {
        this.change('');
        this.synchroCoin();
        this.upInvestLimit();
        this.upApiMoney();
        this.upSlideBar();
      }
    },

    pairNum: { //交易对数量改变，更新投入限制
      deep: true,
      handler (val, oldVal) {
        this.upInvestLimit();
      }
    },
  },

  data() {
    return {
      T:T,
      
      slideParams:undefined, //滑条参数
      moneyNum:'', //可用余额
      minInvest:'', //最小投入
      maxInvest:'', //最大投入
      propose:'', //建议投入
    };
  },

  mounted(){

    this.synchroCoin();
    this.upInvestLimit();
    this.upApiMoney();
    this.upSlideBar();
  },

  methods:{
    
    //检查
    checkError(){
 
      if (this.investCoinnNum.length == 0) {
        modal.toast(T("请输入总投入资金"));
        return true;
      }

      if (math.getNumber(this.investCoinnNum) == 0) {
        modal.toast(T("总投入资金不可为0"));
        return true;
      }
      
      if (math.getNumber(this.investCoinnNum) < math.getNumber(this.minInvest)) {
        modal.toast(T("总投入资金不可低于") + this.minInvest + this.investCoin);
        return true;
      }
      
      if (math.getNumber(this.investCoinnNum) > math.getNumber(this.maxInvest)) {
        modal.toast(T("总投入资金不可高于") + this.maxInvest + this.investCoin);
        return true;
      }
      
      if (math.getNumber(this.investCoinnNum) > math.getNumber(this.moneyNum)) {
        modal.toast(T("可用余额不足"));
        return true;
      }
      
      if (this.api.exchangeName.toLowerCase() == 'uniswap') {

        //现货余额num
        var num = '0';
        var xiaoshu = this.getXiaoshu();
        for (let index = 0; index < this.api.accountAssets.length; index++) {
          var assets = this.api.accountAssets[index];
          if (assets.currency == 'ETH') {
            var value = this.moneyType==2 ? assets.totalNum : assets.availableReal;
            num = math.jia(num, value, xiaoshu, 2);
            break;
          }
        }
        if(math.getNumber(num) < 0.05) {
          modal.toast(T("ETH不足0.05，无法启动"));
          return true;
        }
      }

      return false;
    },

    /********************************** 其他*/   
    //改变-绑定值
    change(value){
      this.investCoinnNum = value
      this.$emit("change", value); 
    },

    //同步API指定币
    synchroCoin() {
      API.synchroCoin(this.api.apiId, this.investCoin);
    },

    /********************************** 更新*/   
    //更新投入限制
    upInvestLimit() {
     
      var limit = mLimit.getInvestLimit(this.type, this.name, this.params, this.investCoin, this.pairMap, this.limitMap, this.pairNum);
      this.minInvest = limit.min;
      this.maxInvest = limit.max;
      this.propose = limit.propose;
    },

    //更新可用数量
    upApiMoney() {
      
      var xiaoshu = this.getXiaoshu();
      var num = '0';
      if (this.api.apiId == '') {
        num = this.maxInvest;
      } else {
        
        // Binance	现货 合约（只有USDT）
        // HuoBi	现货 合约（分币种）
        // Okex		现货 合约(分币种)
        // OkexV5	现货=合约(只取一个)  资金（分币种-现货）

        //现货余额
        for (let index = 0; index < this.api.accountAssets.length; index++) {
          var assets = this.api.accountAssets[index];
          if (assets.currency == this.investCoin) {
            var value = this.moneyType==2 ? assets.totalNum : assets.availableReal;
            num = math.jia(num, value, xiaoshu, 2);
            break;
          }
        }

        //Okexv5资金账户
        var exchangeName = this.api.exchangeName.toLowerCase();
        if (this.moneyType == 2 && exchangeName == 'okexv5') {
          for (let index = 0; index < this.api.moneyAssets.length; index++) {
            var assets = this.api.moneyAssets[index];
            if (assets.currency == this.investCoin) {
              num = math.jia(num, assets.totalNum, xiaoshu, 2);
              break;
            }
          }
        } 
        //合约 API
        else if (this.moneyType == 2) {
           
          var coin = API.isExchangeUSDT(exchangeName) ? this.investCoin : this.pairMap.counterCoin;
          for (let index = 0; index < this.api.linerSwapAssets.length; index++) {
            var assets = this.api.linerSwapAssets[index];
            if (assets.currency == coin) {
              num = math.jia(num, assets.totalNum, xiaoshu, 2);
              break;
            }
          }
        }
      }

      this.moneyNum = math.getNumber(num) < 0 ? '0' : num;

      if (this.isMax) {
        var endStr = math.getNumber(this.maxInvest) > math.getNumber(this.moneyNum) ? this.moneyNum : this.maxInvest;
        this.change(endStr);
      }
    },

    //更新滑条
    upSlideBar(){

      var starStr = '0';
      var endStr = math.getNumber(this.maxInvest) > math.getNumber(this.moneyNum) ? this.moneyNum : this.maxInvest;
      endStr = endStr +'';

      var ratio = this.investCoinnNum.length ? math.chu(this.investCoinnNum, endStr, 2, 2) : '0';
      ratio = math.getNumber(ratio) > 1 ? '1' : ratio;
      ratio = math.getNumber(endStr) == 0 ? 0 : ratio;

      this.slideParams = {'start':starStr, 'end':endStr, 'ratio':ratio};
    },
    
    /**********************************************输入框回调*/
    //输入内容改变
    onInput(e){
      this.change(e.value);
      this.upSlideBar();
    },

    /********************************** GET*/   
    //获取保留小数位
    getXiaoshu() {
      
      var xiaoshu = this.investCoin == this.pairMap.baseCoin ? this.pairMap.baseCoinDecimal : this.pairMap.counterCoinDecimal;
      xiaoshu = math.getNumber(xiaoshu);
      return xiaoshu;
    },

    /********************************** 代理触发*/
    //滑动条-滑动改变
    slideChange(e) {
      var ratio = e.ratio;

      var endStr = math.getNumber(this.maxInvest) > math.getNumber(this.moneyNum) ? this.moneyNum : this.maxInvest;
      if (math.getNumber(endStr) == 0) {
        this.change('0');
        return;
      }

      //保留位数
      var xiaoshu = this.getXiaoshu();    
      var investCoinnNum = math.cheng(endStr, ratio, xiaoshu, 2);
      this.change(investCoinnNum);
    },
  }
}
</script>



<style src="@/_css/style.css"></style>
<style scoped>
.input {
  margin-bottom: 10px;
}
.slide{
  height:160px;
}
</style>
